﻿using MeetAI.Model;
using MeetAI.Model.DTO;
using MeetAI.Model.VO;
using MeetAI.Repository.IRepository;
using MeetAI.Service.IService;
using MeetAI.Utility;
using MeetAI.Utility.Table;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MeetAI.Service
{
    public class Dy_Product_Service : BaseService<dy_product>, IDy_Product_Service
    {
        private readonly IDy_Product_Repository _repository;
        private readonly IDy_ClassType_Repository _class_repository;
        public Dy_Product_Service(IDy_Product_Repository repository, IDy_ClassType_Repository class_repository) : base(repository)
        {
            _repository = repository;
            _class_repository = class_repository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<IEnumerable<dy_product_vo>> getIndexProductAsync(dy_product_dto dto)
        {
            var where = PredicateBuilder.True<dy_product>();
            var tids = "1,2".Split(',').Select(s => int.Parse(s)).ToList();
            //按客片/样片 大分类查询
            if (!dto.tid.HasValue || !tids.Contains(dto.tid.Value))
            {
                return default(List<dy_product_vo>);
            }
            var classTypeIds = _class_repository.FindListByClause(s => s.pid.Equals(dto.tid)).Select(s => s.tid);
            where = where.And(s => classTypeIds.Contains(s.tid));
            var list = await _repository.FindListNoTotalByClauseAsync(where, dto.order, dto.page.Value, dto.size.Value);
            return list.Select(s => new dy_product_vo
            {
                id = s.id,
                title = s.title,
                litpic = string.Format("{0}{1}", staticDomain, s.litpic),
                tid = s.tid,
                time = s.addtime
            });
        }

        public async Task<IPagedList<dy_product_vo>> getPageProductAsync(dy_product_dto dto)
        {
            var where = PredicateBuilder.True<dy_product>();
            var tids = "1,2".Split(',').Select(s => int.Parse(s)).ToList();
            //按客片/样片 大分类查询
            if (dto.tid.HasValue && tids.Contains(dto.tid.Value) && !dto.cid.HasValue)
            {
                var classTypeIds = _class_repository.FindListByClause(s => s.pid.Equals(dto.tid)).Select(s => s.tid);
                where = where.And(s => classTypeIds.Contains(s.tid));
            }
            //具体某个类别查询
            if (dto.cid.HasValue)
            {
                where = where.And(s => s.tid.Equals(dto.cid));
            }
            where = where.And(s=>s.isshow.Equals(1));
            var list = await _repository.FindPagedListAsync(where, dto.order, dto.page.Value, dto.size.Value);
            var simple = list.Select(s => new dy_product_vo
            {
                id = s.id,
                title = s.title,
                litpic = string.Format("{0}{1}", staticDomain, s.litpic),
                tid = s.tid,
                time = s.addtime
            });
            return new PagedList<dy_product_vo>(simple, dto.page.Value, dto.size.Value, list.TotalCount);
        }
    }
}
